﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using VIBlend.WPF.Controls;

namespace AnonsaiWPF.CSharp.TabControl
{
    /// <summary>
    /// Interaction logic for TabControlScrolling.xaml
    /// </summary>
    public partial class TabControlFlexibleStyling : Window, IWindow
    {
        public TabControlFlexibleStyling()
        {
            InitializeComponent();
            Style hierarchyItemElementStyle = (Style)this.Resources["ItemStyle"];
            Style hierarchyItemElementStyle2 = (Style)this.Resources["ItemStyle2"];
            Style hierarchyItemElementStyle3 = (Style)this.Resources["ItemStyle3"];
            pivotGrid1.RowsHierarchy.HierarchyItemDefaultStyle = hierarchyItemElementStyle;
            pivotGrid1.ColumnsHierarchy.HierarchyItemDefaultStyle = hierarchyItemElementStyle;

            pivotGrid2.RowsHierarchy.HierarchyItemDefaultStyle = hierarchyItemElementStyle2;
            pivotGrid2.ColumnsHierarchy.HierarchyItemDefaultStyle = hierarchyItemElementStyle2;

            pivotGrid3.RowsHierarchy.HierarchyItemDefaultStyle = hierarchyItemElementStyle3;
            pivotGrid3.ColumnsHierarchy.HierarchyItemDefaultStyle = hierarchyItemElementStyle3;

            this.LoadGrid(this.pivotGrid1, "Andrew Fuller");
            this.LoadGrid(this.pivotGrid2, "Nancy Davolio");
            this.LoadGrid(this.pivotGrid3, "Regina Murphy");
        }

        private void LoadGrid(VIBlend.WPF.Controls.DataGrid pivotGrid, string name)
        {
            this.PrepareGridData(name);
            pivotGrid.RowsHierarchy.CompactStyleRenderingEnabled = false;
            pivotGrid.ItemsSource = lst;
            pivotGrid.RowsHierarchy.AutoResize(AutoResizeMode.FIT_ITEM_CONTENT);
            pivotGrid.ColumnsHierarchy.AllowResize = false;
            pivotGrid.RowsHierarchy.AllowResize = false;
            pivotGrid.Refresh();
            pivotGrid.RowsHierarchy.ExpandAllItems();
            pivotGrid.RowsHierarchy.Items[0].Width = 110;
        }

        int rowsToLoad = 300;

        #region Sample Data

        #region Data
        string[] firstNames = new string[] 
                {
                    "Andrew",
                    "Nancy",
                    "Shelley",
                    "Regina",
                };

        string[] lastNames = new string[] 
                {
                    "Fuller",
                    "Davolio",
                    "Burke",
                    "Murphy",
                };

        static string[,] productNames = new string[,] 
                {
                    {"Black Tea", "1.95"},
                    {"Green Tea", "1.95"},
                    {"Caffè Espresso", "1.45"},
                    {"Doubleshot Espresso", "1.75"},
                    {"Caffè Latte", "2.25"},
                    {"White Chocolate Mocha", "2.35"},
                    {"Caramel Latte", "2.35"},
                    {"Caffè Americano", "1.65"},
                    {"Cappuccino", "2.10"},
                    {"Espresso con Panna", "1.81"},
                    {"Peppermint Mocha Twist", "1.99"}
                };
        #endregion

        public class EmployeeSales
        {
            public EmployeeSales(string imageUrl, string Name, DateTime Date, int ProductId, int Quantity)
            {
                this.ImageUrl = imageUrl;
                this.Name = Name;
                this.Date = Date;
                this.ProductId = ProductId;
                this.Quantity = Quantity;
            }

            #region Private Members
            private string _imageUrl;
            private string _name;
            private DateTime _date;
            private int _productid;
            private int _quantity;
            #endregion

            #region Properties
            public string ImageUrl
            {
                get { return _imageUrl; }
                set { _imageUrl = value; }
            }


            public string Name
            {
                get { return _name; }
                set { _name = value; }
            }

            public DateTime Date
            {
                get { return _date; }
                set { _date = value; }
            }

            public string ProductName
            {
                get { return productNames[_productid, 0]; }
                set
                {
                    for (int i = 0; i < productNames.Length / 2; i++)
                        if (productNames[i, 0] == value)
                        {
                            _productid = i;
                            break;
                        }
                }

            }

            public double SalesAmount
            {
                get { return (double)_quantity * UnitPrice; }
            }

            public double UnitPrice
            {
                get
                {
                    double unitPrice = double.Parse(productNames[_productid, 1]);
                    return unitPrice;
                }
            }

            public int Quantity
            {
                get { return _quantity; }
                set { _quantity = value; }
            }

            public int ProductId
            {
                get { return _productid; }
                set { _productid = value; }
            }

            #endregion
        }

        public List<EmployeeSales> lst = new List<EmployeeSales>();

        private void PrepareGridData(string name)
        {
            lst.Clear();
            if (lst.Count < rowsToLoad)
            {
                Random rand = new Random();

                for (int i = 0; i < rowsToLoad; i++)
                {
                    int productId = rand.Next(0, 10);
                    int quantity = rand.Next(1, 5);
                    lst.Add(new EmployeeSales("new.jpg",
                        name,
                        DateTime.Now.AddDays(-rand.Next(10, 100)),
                        productId,
                        quantity)
                        );
                }
            }
        }
        #endregion

        private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.TabControl == null)
                return;

            switch (this.TabControl.SelectedIndex)
            {
                case 0:
                    this.TabItem1.Padding = new Thickness(7);
                    this.TabItem2.Padding = new Thickness(5);
                    this.TabItem3.Padding = new Thickness(5);
                    this.TabItem1.FontWeight = FontWeights.Bold;
                    this.TabItem2.FontWeight = FontWeights.Normal;
                    this.TabItem3.FontWeight = FontWeights.Normal;

                    this.TabControl.Background = new SolidColorBrush(Color.FromArgb(255, 222, 183, 201));
                    break;
                case 1:
                    this.TabItem1.Padding = new Thickness(5);
                    this.TabItem2.Padding = new Thickness(7);
                    this.TabItem3.Padding = new Thickness(5);
                    this.TabItem1.FontWeight = FontWeights.Normal;
                    this.TabItem2.FontWeight = FontWeights.Bold;
                    this.TabItem3.FontWeight = FontWeights.Normal;

                    this.TabControl.Background = new SolidColorBrush(Color.FromArgb(255, 255, 233, 172));
                    break;
                case 2:
                    this.TabItem1.Padding = new Thickness(5);
                    this.TabItem2.Padding = new Thickness(5);
                    this.TabItem3.Padding = new Thickness(7);
                    this.TabItem1.FontWeight = FontWeights.Normal;
                    this.TabItem2.FontWeight = FontWeights.Normal;
                    this.TabItem3.FontWeight = FontWeights.Bold;

                    this.TabControl.Background = new SolidColorBrush(Color.FromArgb(255, 189, 170, 226));
                    break;
            }
        }

        public void DetachContent()
        {
            RemoveLogicalChild(Content);
        }

        public void SetTheme()
        {
        }

        public Grid Root
        {
            get
            {
                return this.RootVisual;
            }
        }
    }
}
